package com.library.dao;

import com.library.config.DBConnection;
import com.library.model.BorrowInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class BorrowDAO {
    public boolean insert(String readerNumber, String bookNumber) {
        String sql = "INSERT INTO borrow_book (reader_number, book_number, borrow_time) VALUES (?,?,?)";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, readerNumber);
            pstmt.setString(2, bookNumber);
            pstmt.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public int getBorrowCount(String readerNumber) {
        String sql = "SELECT COUNT(*) FROM borrow_book WHERE reader_number =?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, readerNumber);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt(1);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public boolean deleteBorrowRecord(String readerNumber, String bookNumber) {
        String sql = "DELETE FROM borrow_book WHERE reader_number =? AND book_number =?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, readerNumber);
            pstmt.setString(2, bookNumber);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<BorrowInfo> getBorrowInfoByReader(String readerNumber) {
        List<BorrowInfo> list = new ArrayList<>();
        String sql = "SELECT r.reader_number, b.book_number, b.book_name, b.publisher, bb.borrow_time " +
                "FROM borrow_book bb " +
                "JOIN reader r ON bb.reader_number = r.reader_number " +
                "JOIN book b ON bb.book_number = b.book_number " +
                "WHERE r.reader_number =?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, readerNumber);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    list.add(new BorrowInfo(
                            rs.getString("reader_number"),
                            rs.getString("book_number"),
                            rs.getString("book_name"),
                            rs.getString("publisher"),
                            rs.getTimestamp("borrow_time")
                    ));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}